package cn.Mmovie.servlet;

import cn.Mmovie.bean.Auditorium;
import cn.Mmovie.bean.Cinema;
import cn.Mmovie.bean.Movie;
import cn.Mmovie.bean.Schedule;
import cn.Mmovie.service.AuditoriumService;
import cn.Mmovie.service.ScheduleService;
import cn.Mmovie.service.impl.AuditoriumServiceImpl;
import cn.Mmovie.service.impl.ScheduleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;

@WebServlet(name = "AdminModifyScheduleServlet", value = "/admin/modify_schedule")
public class AdminModifyScheduleServlet extends HttpServlet {
    private ScheduleService scheduleService=new ScheduleServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("-------ModifySchedule----------");
        request.setCharacterEncoding("utf-8");
        Integer id=Integer.parseInt(request.getParameter("id"));
        Time startTime= Time.valueOf(request.getParameter("actTime")+":00");
        Date startDate= Date.valueOf(request.getParameter("actDate"));
        Schedule schedule=new Schedule(id,new Movie(Integer.parseInt(request.getParameter("movie"))),new Auditorium(Integer.parseInt(request.getParameter("auditorium"))),startTime,startDate);
        System.out.println(schedule);
        Integer result=scheduleService.modifySchedule(schedule);
        if (result>0){
            request.setAttribute("msg","放映安排修改成功！");
            request.getRequestDispatcher("/admin/schedule_list").forward(request,response);
        }else if (result==-1){
            request.setAttribute("failMsg","需要修改的放映安排已不存在！");
            request.getRequestDispatcher("/admin/schedule_list").forward(request,response);
        }else {
            request.setAttribute("failMsg","放映安排修改失败，请重试！");
            request.getRequestDispatcher("/admin/schedule_list").forward(request,response);
        }
    }
}
